package com.xuecheng.checkcode.service.impl;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.xuecheng.base.utils.EncryptUtil;
import com.xuecheng.checkcode.model.CheckCodeParamsDto;
import com.xuecheng.checkcode.model.CheckCodeResultDto;
import com.xuecheng.checkcode.service.AbstractCheckCodeService;
import com.xuecheng.checkcode.service.CheckCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 图片验证码生成器
 */
@Slf4j
@Service("PicCheckCodeService")
public class PicCheckCodeServiceImpl extends AbstractCheckCodeService implements CheckCodeService {
    @Autowired
    private DefaultKaptcha kaptcha;

    @Resource(name="NumberLetterCheckCodeGenerator")
    @Override
    public void setCheckCodeGenerator(CheckCodeGenerator checkCodeGenerator) {
        this.checkCodeGenerator = checkCodeGenerator;
    }

    @Resource(name="UUIDKeyGenerator")
    @Override
    public void setKeyGenerator(KeyGenerator keyGenerator) {
        this.keyGenerator = keyGenerator;
    }


    @Resource(name="RedisCheckCodeStore")
    @Override
    public void setCheckCodeStore(CheckCodeStore checkCodeStore) {
        this.checkCodeStore = checkCodeStore;
    }

    @Override
    public CheckCodeResultDto generate(CheckCodeParamsDto checkCodeParamsDto) {
        GenerateResult result = generate(checkCodeParamsDto,4,"checkcode:",300);
        String key = result.getKey();
        String code = result.getCode();
        String pic = createPic(code);
        CheckCodeResultDto resultDto = new CheckCodeResultDto();
        resultDto.setKey(key);
        resultDto.setAliasing(pic);
        return resultDto;
    }

    private String createPic(String code){
        //生成图片验证码
        ByteArrayOutputStream outputStream = null;
        BufferedImage image = kaptcha.createImage(code);
        outputStream = new ByteArrayOutputStream();
        String imgBase64Encoder = null;
        try{
            //对字节数组Base64编码
            BASE64Encoder base64Encoder = new BASE64Encoder();
            ImageIO.write(image,"png",outputStream);
            imgBase64Encoder = "data:image/png;base64," + EncryptUtil.encodeBase64(outputStream.toByteArray());
        }catch(IOException e){
            e.printStackTrace();
        }finally{
            try{
                outputStream.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return imgBase64Encoder;
    }

    /**
     * 生成字符串验证码
     * @param dto
     * @return
     */
    @Override
    public CheckCodeResultDto generateCode(CheckCodeParamsDto dto) {
        String type = dto.getCheckCodeType();
        String param1 = dto.getParam1();
        //生成验证码
        String code = checkCodeGenerator.generate(4);
        log.info("生成验证码:{}",code);
        //生成一个key
        String key = "checkcode:" + type + ":" + param1;
        //存储验证码
        checkCodeStore.set(key,code,300);
        CheckCodeResultDto resultDto = new CheckCodeResultDto();
        resultDto.setKey(key);
        resultDto.setCode(code);
        return resultDto;
    }
}
